package com.neusoft.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.neusoft.dto.PositionConditionDto;
import com.neusoft.dto.PositionDto;
import com.neusoft.mapper.PositionMapper;
import com.neusoft.model.Position;
import com.neusoft.service.PositionService;
import com.neusoft.utils.ServerResponse;
import com.neusoft.vo.PositionInfoVo;
import com.neusoft.vo.PositionVo;
import com.neusoft.vo.PositionsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @AUTHOR 杜豪冰
 * @DATE 2025/1/8 16:52
 * @DESCRIBER
 */
@Service
public class PositionServiceImpl implements PositionService {
    @Autowired
    PositionMapper positionMapper;

    @Override
    public ServerResponse getPositions(PositionConditionDto positionConditionDto) {
        System.out.println("---"+positionConditionDto);
        if(positionConditionDto.getLimit() == null){
            positionConditionDto.setLimit(20);
        }
        if(positionConditionDto.getOffset() == null){
            positionConditionDto.setOffset(0);
        }
        if(positionConditionDto.getStatus() != null){

            if(positionConditionDto.getStatus().equals("活动")) {
                positionConditionDto.setStatus("1");
            } else{
                positionConditionDto.setStatus("0");
            }
        }
        PositionsVo positions = new PositionsVo();
        positions.setTotal(positionMapper.getPositionsCount(positionConditionDto));
        List<PositionVo> positionsList = positionMapper.getPositions(positionConditionDto);
        for (int i = 0; i < positionsList.size(); i++) {
            if(positionsList.get(i).getStatus().equals("1"))
                positionsList.get(i).setStatus("活动");
            else{
                positionsList.get(i).setStatus("撤销");
            }
        }
        positions.setPositions(positionsList);
        return ServerResponse.success(positions);
    }

    @Override
    public ServerResponse addPosition(PositionDto positionDto) {
        //判断该名称是否已存在
        QueryWrapper<Position> queryWrapperName = new QueryWrapper<>();
        queryWrapperName.eq("name", positionDto.getName());
        if(positionMapper.selectOne(queryWrapperName)!=null)
            return ServerResponse.error("该职位已存在");
        else{

            Position position = new Position();
            position.setName(positionDto.getName());
            position.setRank(positionDto.getRank());

            position.setNo((int) (positionMapper.selectCount(null)+1));
            positionMapper.insert(position);
            return ServerResponse.success("添加成功");
        }
    }

    @Override
    public ServerResponse modifyPosition(PositionDto positionDto , Integer no) {
        QueryWrapper<Position> queryWrapperNo = new QueryWrapper<>();
        queryWrapperNo.eq("no", no);
        QueryWrapper<Position> queryWrapperName = new QueryWrapper<>();
        queryWrapperName.eq("name", positionDto.getName());
        Position position = positionMapper.selectOne(queryWrapperNo);
        //判断该名称是否已存在
        if(!positionDto.getName().equals(position.getName())
                && positionMapper.selectOne(queryWrapperName)!=null){
            return ServerResponse.error("该职位已存在");
        }
            Position newPosition = new Position();
            newPosition.setName(positionDto.getName());
            newPosition.setRank(positionDto.getRank());

            UpdateWrapper<Position> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("no", no); // 设置更新条件

            positionMapper.update(newPosition,updateWrapper);
            return ServerResponse.success("修改成功");
        }

    @Override
    public ServerResponse getPositionDetail(Integer no) {
        PositionInfoVo positionInfoVo = new PositionInfoVo();
        positionInfoVo.setEmps(positionMapper.getPstEmpsDetail(no));
        QueryWrapper<Position> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("no", no);
        Position position = positionMapper.selectOne(queryWrapper);
        positionInfoVo.setNo(position.getNo());
        positionInfoVo.setName(position.getName());
        positionInfoVo.setRank(position.getRank());
        if(position.getStatus().equals("1"))
            positionInfoVo.setStatus("活动");
        else {
            positionInfoVo.setStatus("撤销");
        }
        return ServerResponse.success(positionInfoVo);
    }

    @Override
    public ServerResponse deletePositionDetail(Integer no) {
        QueryWrapper<Position> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("no", no);
        int result = positionMapper.delete(queryWrapper);
        if(result == 0)
            return ServerResponse.error("改职位不存在");
        return ServerResponse.success("删除成功");
    }
}
